﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Web;

namespace MemberShip.Models
{
    public class StudioMemberShipContext : DbContext
    {
        public StudioMemberShipContext()
            : base("DefaultConnection")
        {
        }

        public DbSet<UserProfile> UserProfiles { get; set; }
        public DbSet<StudioMemberShip> StudioMemberShips { get; set; }
        public DbSet<Role> Roles { get; set; }
        public DbSet<Permission> Permissions { get;set;}
        public DbSet<PermissionsInRoles> PermissionsInRoles { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<StudioMemberShip>().HasMany<Role>(r=>r.Roles).WithMany(u=>u.Members).Map(m =>
            {
                m.ToTable("webpages_UsersInRoles");
                m.MapLeftKey("UserId");
                m.MapRightKey("RoleId");
            });
        }
    }

    [Table("webpages_Roles")]
    public class Role
    {
        public Role()
        {
            Members=new List<StudioMemberShip>();
            PermissionsInRoles=new List<PermissionsInRoles>();
        }

        [Key]
        [Display(Name = "ID")]
        public int RoleId { get; set; }

        [Display(Name = "名称")]
        [StringLength(256)]
        public string RoleNmae { get; set; }

        public ICollection<StudioMemberShip> Members { get; set; }

        public ICollection<PermissionsInRoles> PermissionsInRoles { get; set; }

    }
    [Table("webpages_Membership")]
    public class StudioMemberShip
    {
        public StudioMemberShip()
        {
            Roles=new List<Role>();
        }
        [Key,DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int UserId { get; set; }
        public DateTime? CreateDate { get; set; }
        [StringLength(128)]
        public string ConfirmationToken { get; set; }
        public bool? IsConfirmed { get; set; }
        public DateTime? LastPasswordFailureDate { get; set; }
        public int PasswordFailuresSinceLastSuccess { get; set; }
        [Required,StringLength(128)]
        public string Password { get; set; }
        public DateTime? PasswordChangedDate { get; set; }
        public string PasswordSalt { get; set; }
        [StringLength(128)]
        public string PasswordVerificationToken { get; set; }
        public DateTime? PasswordVerificationTokenExpirationDate { get; set; }
        [StringLength(128)]
        public string PasswordResetToken { get; set; }
        public ICollection<Role> Roles { get; set; }
    }
    [Table("webpages_Permission")]
    public class Permission
    {
        public Permission()
        {
            PermissionsInRoles=new List<PermissionsInRoles>();
        }
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        [Display(Name = "权限ID")]
        public int PermissionId { get; set; }
        [Display(Name = "名称")]
        public string PermissionName { get; set; }
        [ForeignKey("PermissionId")]
        public ICollection<PermissionsInRoles> PermissionsInRoles { get; set; }
    }
    [Table("webpages_PermissionsInRoles")]
    public class PermissionsInRoles
    {
        [Key]
        public int Id { get; set; }
        public int RoleId { get; set; }
        public int PermissionId { get; set; }
        [Column("RoleId"),InverseProperty("PermissionsInRoles")]
        public Role Role { get; set; }
        [Column("PermissionId"),InverseProperty("PermissionsInRoles")]
        public Permission Permission { get; set; }
    }


}